#JT Hamrick
#Script for analyzing data on rise and fall of cryptocurrency coins.
#Gandal, N., Hamrick, J.T., Moore, T. et al. The rise and fall of cryptocurrency coins and tokens. Decisions Econ Finan (2021). https://doi.org/10.1007/s10203-021-00329-8


options(width=as.integer(system("stty -a | head -n 1 | awk '{print $6}'", intern=T)))
options(scipen=999)
library(data.table)
library(survival)
library(zoo)
library(dplyr)
library(Hmisc)
library(lubridate)

ac<-fread("abandonrangesuniqWEIS18v2.csv")
pv<-fread("volume_peaks.csv")
pp<-fread("price_peaks.csv")
moav<-fread("moavol.csv")
abmo<-fread("abmo.csv")
cv<-fread("token_data.csv")
bitcoin <- fread("bitcoin_data.csv")
coins.avgvol <- fread("coin_avgvol.csv")
ppp<-fread("price_prepost.csv")
vpp<-fread("volume_prepost.csv")

# convert column types and make date columns
ac$peakday<-as.Date(ac$peakday,"%Y-%m-%d")
ac$peakmon<-cut(ac$peakday,breaks="month")
ac$peakday<-as.Date(ac$peakday,"%Y-%m-%d")
ac$peakmon<-cut(ac$peakday,breaks="month")
ac$resmonth<-as.Date(ac$resmon)

pp$peakd<-as.Date(pp$peak,"%Y-%m-%d")
pp$peakmon<-cut(pp$peakd,breaks="month")
pv$peakd<-as.Date(pv$peak,"%Y-%m-%d")
pv$peakmon<-cut(pv$peakd,breaks="month")

# subset ac data and calculate resurrections per month
acr<-ac[ac$resurrected,]
numres<-aggregate(resvol~resmonth,length,data=acr)
names(numres)<-c("month","numres")

# convert dates to factors
moav$mon<-factor(as.Date(moav$mo,"%Y-%m-%d"))

# find number of active coins per month
numcoins<-aggregate(moavol~mon,length,data=moav)
names(numcoins)<-c("month","numcoins")
# convert month to date object
numcoins$month<-as.Date(numcoins$month,"%Y-%m-%d")

# find number of abandonments per month
abmo$abmonth<-as.Date(abmo$abmonth,"%Y-%m-%d")
numab<-aggregate(coin~abmonth,length,data=abmo)
names(numab)<-c("month","numabcoins")

# merge active and abandoned datasets
alivedead<-merge(numcoins,numab,by="month",all.x=T)
# convert NA values to 0
alivedead$numabcoins[is.na(alivedead$numabcoins)]<-0
# find number of "alive" coins per month
alivedead$numalive<-alivedead$numcoins-alivedead$numabcoins

nc2<-aggregate(moavol~mon+market,length,data=moav)
nc2$mon<-as.Date(nc2$mon)
abmo$null<-1
na2<-aggregate(null~abmonth+coin,length,data=abmo)
nac3<-merge(nc2,na2,by.x=c("market","mon"),by.y=c("coin","abmonth"))
abactive<-aggregate(moavol~mon,length,data=nac3)
names(abactive)<-c("month","numactiveab")

alivedead<-merge(alivedead,abactive,by="month", all=T)
alivedead$numactiveab[is.na(alivedead$numactiveab)]<-0
alivedead$numcoinsad<-alivedead$numcoins+alivedead$numabcoins-alivedead$numactiveab
alivedead$frab<-alivedead$numabcoins/alivedead$numcoinsad

numpeakp<-aggregate(peak_value~peakmon,length,data=pp)
names(numpeakp)<-c("month","numpeakprice")
numpeakp$month<-as.Date(numpeakp$month,"%Y-%m-%d")

numpeakv<-aggregate(peak_value~peakmon,length,data=pv)
names(numpeakv)<-c("month","numpeakvol")
numpeakv$month<-as.Date(numpeakv$month,"%Y-%m-%d")

numpeaks<-merge(numcoins,numpeakp,by="month", all=T)
numpeaks$numpeakprice[is.na(numpeaks$numpeakprice)] <- 0
numpeaks<-merge(numpeaks,numpeakv,by="month", all=T)
numpeaks$numpeakvol[is.na(numpeaks$numpeakvol)] <- 0
ad2<-alivedead[,c("month","numalive")]
numpeaks<-merge(numpeaks,ad2,by="month",all.x=T)
numpeaks$frpeakprice<-numpeaks$numpeakprice/numpeaks$numalive
numpeaks$frpeakvol<-numpeaks$numpeakvol/numpeaks$numalive

# convert token data column types
cv$market_cap <- as.numeric(gsub(",", "", cv$market_cap))
cv$volume <- as.numeric(gsub(",", "", cv$volume))
cv$date <- as.Date(cv$date)
# subset data to end in February (to match coin analysis)
cv <- cv[date<=as.Date("2018-02-06"),]

# get first record for each coin
cvfd <- cv[cv[, .I[which.min(date)], by=market]$V1]
cvfd <- subset(cvfd, select=-c(tag, unix_time, high, low, close, market_cap, platform, id))
colnames(cvfd) <- c("market", "fdate", "fopen", "fvolume")

pv.first <- pv[pv[, .I[which.min(peakd)], by=market]$V1]
pp.first <- pp[pp[, .I[which.min(peakd)], by=market]$V1]

pv.first <- subset(pv.first, select=-c(run_type, peak))
pp.first <- subset(pp.first, select=-c(run_type, peak))
colnames(pv.first) <- c("market", "v_peak_value", "v_peakd", "v_peakmon")
colnames(pp.first) <- c("market", "p_peak_value", "p_peakd", "p_peakmon")

coinsummary <- cvfd
coinsummary <- merge(coinsummary, pp.first, by.x="market", by.y="market", all=T)
coinsummary <- merge(coinsummary, pv.first, by.x="market", by.y="market", all=T)

# calculate time to first peak
coinsummary$p_dtfp <- as.numeric(coinsummary$p_peakd - coinsummary$fdate)
coinsummary$v_dtfp <- as.numeric(coinsummary$v_peakd - coinsummary$fdate)
# convert column type to numeric
coinsummary$fopen <- as.numeric(coinsummary$fopen)

# get percent increase (1st day to peak)
coinsummary$p_peak_value <- as.numeric(coinsummary$p_peak_value)
coinsummary$p_pcti <- (coinsummary$p_peak_value - coinsummary$fopen) / coinsummary$fopen
coinsummary$v_pcti <- (coinsummary$v_peak_value - coinsummary$fvolume) / coinsummary$fvolume

# get time to 1st abandonment
ac$abmon <- as.Date(ac$abmon)
ac.firstab <- ac[ac[, .I[which.min(abmon)], by=coin]$V1]
coinsummary$res <- FALSE
coinsummary$resmon <- character()
coinsummary$fab <- character()
for (item in 1:nrow(coinsummary)) {
    index = which(ac.firstab$coin == coinsummary[item]$market)
    if (length(index) > 0) {
        # convert date to string or R will give you some useless number
        coinsummary[item]$fab <- as.character(ac.firstab$abmon[index])
        coinsummary[item]$res <- as.character(ac.firstab$resurrected[index])
        coinsummary[item]$resmon <- as.character(ac.firstab$resmon[index])
    }
}

# create month column
coinsummary$fmo <- cut(coinsummary$fdate, breaks="month")
# convert column types
coinsummary$fab <- as.Date(coinsummary$fab)
coinsummary$resmon <- as.Date(coinsummary$resmon)
coinsummary$fmo <- as.Date(coinsummary$fmo)
coinsummary$v_peakmon <- as.Date(coinsummary$v_peakmon)

# open to first abandonment
coinsummary$otfa <- as.numeric(coinsummary$fab - coinsummary$fmo)

# peak to first abandonment
coinsummary$ptfa <- 12 * as.numeric(as.yearmon(coinsummary$fab) - as.yearmon(coinsummary$v_peakmon))
coinsummary$resmon <- as.Date(coinsummary$resmon)
coinsummary$ttrpa <- 12 * as.numeric(as.yearmon(coinsummary$resmon) - as.yearmon(coinsummary$v_peakmon))
# reset coins not resurrected
coinsummary$ttrpa[which(coinsummary$res==FALSE)] <- NA

pv <- subset(pv, select=-c(peak, run_type))
colnames(pv) <- c("market", "v_peak_value", "v_peakd", "v_peakmon")

# get dataset without first peak rows
pv.second <- anti_join(pv, pv.first, by=c("market", "v_peakd"))
pv.second <- data.table(pv.second)
pv.second <- pv.second[pv.second[, .I[which.min(v_peakd)], by=market]$V1]
colnames(pv.second) <- c("market", "pa_peak_value", "pa_peakd", "pa_peakmon")

cs<-coinsummary
cs$openday<-as.Date(cs$fdate,"%Y-%m-%d")
cs$openmo<-cut(cs$openday,breaks="month")

numopen<-aggregate(fopen~openmo,length,data=cs)
names(numopen)<-c("month","newcoins")
numopen$month<-as.Date(numopen$month,"%Y-%m-%d")

numpeaks<-merge(numopen,numpeaks,by="month", all=T)
numpeaks$newcoins[is.na(numpeaks$newcoins)] <- 0

numab<-aggregate(abvol~abmon,length,data=ac)
names(numab)<-c("month","numab")
numab$month<-as.Date(numab$month,"%Y-%m-%d")

numpeaks<-merge(numab,numpeaks,by="month",all.y=T)
numpeaks$numab[is.na(numpeaks$numab)] <- 0

# convert column types
bitcoin$date <- as.Date(bitcoin$date, "%d-%b-%y")
bitcoin$open <- as.numeric(gsub(",","",bitcoin$open))
bitcoin$high <- as.numeric(gsub(",","",bitcoin$high))
bitcoin$low <- as.numeric(gsub(",","",bitcoin$low))
bitcoin$close <- as.numeric(gsub(",","",bitcoin$close))
bitcoin$volume <- as.numeric(gsub(",","",bitcoin$volume))
bitcoin$marketcap <- as.numeric(gsub(",","",bitcoin$marketcap))

# convert column type
coins.avgvol$date <- as.Date(coins.avgvol$date, "%Y-%m-%d")

numrespeak <- merge(numpeaks,numres,by="month",all=T)
numrespeak$numres[is.na(numrespeak$numres)] <- 0

pdf('coinabf.pdf',height=9,width=8)
par(mfrow=c(5,1), oma = c(2.1,2,0,2), mar = c(2,3,1.5,3))
plot(x=numpeaks$month,y=numpeaks$newcoins, type="l", main="Tokens Newly Created/Abandoned", xlab="Date", ylab="# Tokens", col="black", ylim=c(0, max(numpeaks$newcoins,na.rm=T)), cex.lab=1.25,xlim=c(min(numpeaks$month),max(numpeaks$month)))
lines(x=numpeaks$month,y=numpeaks$numab, col="red",lty=2)
legend("topleft", legend=c("newly created", "newly abandoned"), col=c("black", "red"), lty=1:2, bg="white", cex=1.25)

plot(x=numres$month,y=numres$numres, col="blue", type="l", xlab="Date", ylab="# Tokens", main="Tokens Resurrected/Avg. Daily Trans Vol", cex.lab=1.25,xlim=c(min(numpeaks$month),max(numpeaks$month)),lty=1)
mtext(side = 2, line = 3, '# Tokens', cex.lab=1.25)
par(new = T)
plot(coins.avgvol$date, coins.avgvol$avgvol, pch=16, axes=F, xlab=NA, ylab=NA, col="darkgreen", type="l",xlim=c(min(numpeaks$month),max(numpeaks$month)),lty=2)
axis(side = 4)
mtext(side = 4, line = 3, 'Daily Avg. Trading Volume Per Month', cex.lab=1.25)
legend("topleft", legend=c("resurrected", "avg daily transaction"), col=c("blue", "darkgreen"), lty=1:2, bg="white", cex=1.25)

plot(x=alivedead$month,y=alivedead$numcoinsad,type='l',xlim=c(min(numpeaks$month),max(numpeaks$month)),ylim=c(0,max(alivedead$numcoinsad)), cex.lab=1.2,main="Active vs. Abandoned Tokens")
lines(x=alivedead$month,y=alivedead$numabcoins,type='l',lty=2)
legend("topleft", legend=c("currently active", "currently abandoned"), col=c("black", "black"), lty=1:2, bg="white", cex=1.25)

plot(x=alivedead$month,y=alivedead$frab,type='l',xlim=c(min(numpeaks$month),max(numpeaks$month)),main="Fraction Tokens Abandoned", cex=1.25)

plot(bitcoin$date[bitcoin$date<=as.Date("2018-02-06")], bitcoin$open[bitcoin$date<=as.Date("2018-02-06")], type="l", main="BTC price", cex=1.25, log="y")
dev.off()

pdf('peakstime.pdf',height=7,width=10)
par(mfrow=c(2,1), oma = c(2.1,2,0,2), mar = c(2,1,1.5,1))

plot(x=numpeaks$month,y=numpeaks$numpeakprice, type="l", main="Token Peaks (#)", xlab="Date", ylab="# Tokens", col="brown", ylim=c(0, max(numpeaks$numpeakprice,na.rm=T)), cex.lab=1.25)

lines(x=numpeaks$month,y=numpeaks$numpeakvol, col="orange",lty=2)
legend("topleft", legend=c("# tokens price peak", "# tokens volume peak"), col=c("brown", "orange"), lty=1:2, bg="white", cex=1.25)

plot(x=numpeaks$month,y=100*numpeaks$frpeakprice, type="l", main="Token Peaks (%)", xlab="Date", ylab="# Coins", col="brown", cex.lab=1.25)

lines(x=numpeaks$month,y=100*numpeaks$frpeakvol, col="orange",lty=2)
legend("topleft", legend=c("% tokens price peak", "% tokens volume peak"), col=c("brown", "orange"), lty=1:2, bg="white", cex=1.25)
dev.off()

#identify big, medium, small coins
cv[,obs:=length(date), by=market]
cv[,avgvol:=mean(volume), by=market]

cvs<-aggregate(volume~market,sum,data=cv,na.rm=T)

cvs$size<-ifelse(cvs$volume<1000000,"m1","m1m10")
cvs$size[cvs$volume>=10000000]<-"m10m100"
cvs$size[cvs$volume>=100000000]<-"m100b1"
cvs$size[cvs$volume>=1000000000]<-"b1"

cvs$size<-ordered(cvs$size,c("m1","m1m10","m10m100","m100b1","b1"))
names(cvs)<-c("coin","totalvol","size")

cs2<-merge(cvs,coinsummary,by.x="coin",by.y="market")

acs<-merge(ac,cvs,by="coin",all.y=T)
acs$abandoned[is.na(acs$abandoned)]<-F

cs2$fyear<-cut(cs2$fdate,breaks="year")

cs2$size2<-ifelse(cs2$size %in% c("m1","m1m10","m10m100"),"l100m","g100m")

lastday<-aggregate(date~market,cv,max)
names(lastday)<-c("coin","lastday")
cs2no1wk<-merge(lastday,cs2,by="coin")
cs2no1wk$ttpp<-as.Date(cs2no1wk$p_peakd)-as.Date(cs2no1wk$fdate)
cs2no1wk$ttvp<-as.Date(cs2no1wk$v_peakd)-as.Date(cs2no1wk$fdate)

cs2no1wkp<-cs2no1wk[cs2no1wk$ttpp>=7,]
cs2no1wkv<-cs2no1wk[cs2no1wk$ttvp>=7,]

yo<-aggregate(p_pcti~fyear,cs2no1wkp,median,na.rm=T)
yo$size2<-"all"
ys<-aggregate(p_pcti~size2+fyear,cs2no1wkp,median,na.rm=T)
ys<-ys[3:10,]
ys2<-rbind(ys,yo[2:5,])
ys2$p_pcti<-100*ys2$p_pcti
ys2l<-ys2[ys2$size2=="l100m",]
ys2g<-ys2[ys2$size2=="g100m",]
ys2a<-ys2[ys2$size2=="all",]

vo<-aggregate(v_pcti~fyear,cs2no1wkv,median,na.rm=T)
vo$size2<-"all"
vs<-aggregate(v_pcti~size2+fyear,cs2no1wkv,median,na.rm=T)
vs<-vs[3:10,]
vs2<-rbind(vs,vo[2:5,])
vs2$v_pcti<-100*vs2$v_pcti
vs2l<-vs2[vs2$size2=="l100m",]
vs2g<-vs2[vs2$size2=="g100m",]
vs2a<-vs2[vs2$size2=="all",]

pdf("yr2peak.pdf",height=4,width=10)
par(mfrow=c(1,2), mar = c(4,4,1.5,0.5))
plot(x=as.Date(ys2g$fyear),y = ys2g$p_pcti,type='l',log='y',ylim=c(min(ys2$p_pcti),max(vs2$v_pcti)),lty=4,col=4,lwd=2,xlab="year token introduced",ylab="median % price increase to 1st peak",main="% Price Increase Start to 1st Peak",cex.lab=1.2,xlim=c(as.Date("2014-01-01"),as.Date("2017-01-01")))
lines(x=as.Date(ys2l$fyear),y = ys2l$p_pcti,type='l',lty=2,col=2,lwd=2)
lines(x=as.Date(ys2a$fyear),y = ys2a$p_pcti,type='l',lty=1,col=1,lwd=2)
legend("topright",c("overall","total vol. < $100M","total vol. > $100M"),col=c(1,2,4),lty=c(1,2,4),lwd=2,cex=1.2)

plot(x=as.Date(vs2g$fyear),y = vs2g$v_pcti,type='l',log='y',ylim=c(min(ys2$p_pcti),max(vs2$v_pcti)),lty=4,col=4,lwd=2,xlab="year token introduced",ylab="median % volume increase to 1st peak",main="% Volume Increase Start to 1st Peak",cex.lab=1.2,xlim=c(as.Date("2014-01-01"),as.Date("2017-01-01")))
lines(x=as.Date(vs2l$fyear),y = vs2l$v_pcti,type='l',lty=2,col=2,lwd=2)
lines(x=as.Date(vs2a$fyear),y = vs2a$v_pcti,type='l',lty=1,col=1,lwd=2)
dev.off()

aban<-acs[,c("coin","abandoned")]
cs3<-merge(aban,cs2,by="coin")

cs3$otfa<-as.numeric(as.Date("2018-02-06")-cs3$fdate)

sab<-Surv(time=cs3$otfa,event=cs3$abandoned,type="right")

ac$months2res<-as.numeric(round((ac$resmonth-ac$abmon)/30))

cssize<-cs2[,c("coin","size")]
acsize<-merge(ac,cssize,by="coin")

sres<-Surv(time=acsize$months2res,event=acsize$abandoned,type="right")

pdf("survabres.pdf",height=6,width=12)
par(mfrow = c(1,2),mar = c(4,4,1.5,0.5))
plot(survfit(sab~size,data=cs3),col=2:6,lty=2:6,lwd=2,ylab="probability token not yet abandoned",xlab="days since token launched",main="Time to Abandonment",cex.lab=1.3,xlim=c(0,2200))
lines(survfit(sab~1),lwd=2,conf.int=F)

plot(survfit(sres~size,data=acsize),col=2:5,lty=2:5,lwd=2,ylab="probability token not yet resurrected",xlab="months since token abandoned",main="Time to Resurrection",cex.lab=1.2,xlim=c(0,60))
lines(survfit(sres~1),lwd=2,conf.int=F)

legend("topright",c("overall","<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol."),col=1:6,lty=1:6,lwd=2,bg="white",cex=1.3)
dev.off()

coinct<-data.frame(table(acsize$coin))
names(coinct)<-c("coin","numabandon")
coinct$numabf<-factor(coinct$numabandon)
coinct2<-merge(coinct,cssize,by="coin")

acrsize<-acsize[acsize$resurrected,]
coinrct<-data.frame(table(acrsize$coin))
names(coinrct)<-c("coin","numresurrect")

coinrct$numresf<-factor(coinrct$numresurrect)
coinrct2<-merge(coinrct,cssize,by="coin")

pdf("numabres.pdf",height=4,width=10)
par(mfrow = c(1,2),mar = c(4,4,1.5,0.5))
barplot(table(coinct2$size,coinct2$numabf),beside=T,col=1:5,density=20,angle=seq(45,225,45),border=T,xlab="# abandonments",ylab="# tokens",main="Abandonments per token")
barplot(table(coinrct2$size,coinrct2$numresf),beside=T,col=1:5,density=20,angle=seq(45,225,45),border=T,xlab="# resurrections",ylab="# tokens",main="Resurrections per token")
legend("topright",c("<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol."),fill=1:5,density=20,angle=seq(45,225,45),border=T)
dev.off()

# convert column type
ppp$peak<-as.Date(ppp$peak, origin="1970-01-01")
ppp<-merge(cssize,ppp,by.x="coin",by.y="market")

# convert column type
ppp$peak_value<-as.numeric(ppp$peak_value)
ppp$pre<-as.numeric(ppp$pre)
ppp$post<-as.numeric(ppp$post)

ppp$peakjump<-ppp$peak_value-ppp$pre
ppp$peakjumpfr<-100*ppp$peakjump/ppp$pre

ppp$peakfall<-ppp$peak_value-ppp$post
ppp$peakfallfr<-100*ppp$peakfall/ppp$peak_value

ppp<-merge(ppp,cvfd,by.x="coin",by.y="market",all.x=T)

ppp$ttpp<-as.Date(ppp$peak)-as.Date(ppp$fdate)

lastday<-aggregate(date~market,cv,max)
names(lastday)<-c("coin","lastday")
ppp<-merge(ppp,lastday,by="coin")
ppp$ttlp<-ppp$lastday-as.Date(ppp$peak)

ppp2<-ppp[ppp$ttpp>=7,]
ppp3<-ppp2[ppp2$ttlp>=7,]

pjf<-aggregate(peakjumpfr~size,ppp2,quantile,seq(0,1,0.1),na.rm=T)
pff<-aggregate(peakfallfr~size,ppp3,quantile,seq(0,1,0.1),na.rm=T)

vpp$peak<-as.Date(vpp$peak, origin="1970-01-01")
vpp<-merge(cssize,vpp,by.x="coin",by.y="market")

vpp$peak_value<-as.numeric(vpp$peak_value)
vpp$pre<-as.numeric(vpp$pre)
vpp$post<-as.numeric(vpp$post)

vpp$peakjump<-vpp$peak_value-vpp$pre
vpp$peakjumpfr<-100*vpp$peakjump/vpp$pre

vpp$peakfall<-vpp$peak_value-vpp$post
vpp$peakfallfr<-100*vpp$peakfall/vpp$peak_value

vpp<-merge(vpp,cvfd,by.x="coin",by.y="market",all.x=T)
vpp$ttpp<-as.Date(vpp$peak)-as.Date(vpp$fdate)

vpp<-merge(vpp,lastday,by="coin")
vpp$ttlp<-vpp$lastday-as.Date(vpp$peak)

vpp2<-vpp[vpp$ttpp>=7,]
vpp3<-vpp2[vpp2$ttlp>=7,]

vpp2 <- vpp2[!is.infinite(vpp2$peakjumpfr),]

vjf<-aggregate(peakjumpfr~size,vpp2,quantile,seq(0,1,0.1),na.rm=T)
vff<-aggregate(peakfallfr~size,vpp3,quantile,seq(0,1,0.1),na.rm=T)

vjf$peakjumpfr[vjf$peakumpfr<1]<-1

pdf("pvpeak.pdf",height=8,width=10)
par(mfrow = c(2,2),mar = c(5,5,4,1),las=1)
barplot(pjf$peakjumpfr[,2:10],beside=T,horiz=T,col=1:5,density=20,angle=seq(45,225,45),border=T,ylab="percentile",xlab="% price increase",main="% price increase from min 30 days prior to peak",cex.names=1.25,cex.axis=1.2,cex.lab=1.2)
legend("bottomright",rev(c("<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol.")),fill=5:1,density=20,angle=rev(seq(45,225,45)),border=T,cex=1.15)
barplot(pff$peakfallfr[,2:10],beside=T,horiz=T,col=1:5,density=20,angle=seq(45,225,45),border=T,ylab="percentile",xlab="% price decrease",main="% price decrease from peak to min 30 days after",cex.names=1.25,cex.axis=1.2,cex.lab=1.2)

barplot(vjf$peakjumpfr[,2:10],beside=T,horiz=T,col=1:5,ylab="percentile",xlab="% volume increase",main="% volume increase from min 30 days prior to peak",log='x',cex.names=1.25,cex.axis=1.2,cex.lab=1.2)
legend("bottomright",rev(c("<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol.")),fill=5:1,cex=1.15)
barplot(vff$peakfallfr[,2:10],beside=T,horiz=T,col=1:5,ylab="percentile",xlab="% volume decrease",main="% volume decrease from peak to min 30 days after",cex.names=1.25,cex.axis=1.2,cex.lab=1.2)
dev.off()

ppkct<-data.frame(table(ppp$coin))
vpkct<-data.frame(table(vpp$coin))
names(ppkct)<-c("coin","numpeaks")
ppkct$numpkf<-factor(ppkct$numpeaks)
ppkct2<-merge(ppkct,cssize,by="coin")

names(vpkct)<-c("coin","numpeaks")
vpkct$numpkf<-factor(vpkct$numpeaks)
vpkct2<-merge(vpkct,cssize,by="coin")

acsu<-unique(acs[acs$abandoned,c("coin","size")])
acsru<-unique(acs[acs$resurrected,c("coin","size")])

pacsu<-unique(acs[acs$abandoned&!acs$resurrected,c("coin","size")])

cs2v2<-merge(cs2,ppp2,by.x=c("coin","p_peakd","size"),by.y=c("coin","peak","size"))
cs2v3<-merge(cs2,vpp2,by.x=c("coin","v_peakd","size"),by.y=c("coin","peak","size"))

sumtbl<-
data.frame(rbind(
c(sum(table(cvs$size)),table(cvs$size)),
c(sum(ppkct2$numpeaks),aggregate(numpeaks~size,ppkct2,sum)$numpeaks),
c(median(ppkct2$numpeaks),aggregate(numpeaks~size,ppkct2,median)$numpeaks),
c(sum(vpkct2$numpeaks),aggregate(numpeaks~size,vpkct2,sum)$numpeaks),
c(median(vpkct2$numpeaks),aggregate(numpeaks~size,vpkct2,median)$numpeaks),
round(c(100*median(cs2v2$p_pcti,na.rm=T),100*aggregate(p_pcti~size,cs2v2,median,na.rm=T)$p_pcti),0),
round(c(100*median(cs2v3$v_pcti,na.rm=T),100*aggregate(v_pcti~size,cs2v3,median,na.rm=T)$v_pcti),0),
c(length(acsu$coin),table(acsu$size)),
round(100*c(length(acsu$coin)/sum(table(cvs$size)),table(acsu$size)/table(cvs$size),0)),
c(length(acs$abandoned[acs$abandoned]),table(acs$abandoned,acs$size)[2,]),
round(c(median(cs3$otfa[cs3$abandoned],na.rm=T),aggregate(otfa~size,cs3[cs3$abandoned],median,na.rm=T)$otfa),0),
c(length(acsru$coin),table(acsru$size)),
round(100*c(length(acsru$coin)/length(acsu$coin),table(acsru$size)/table(acs$size)),0),
c(summary(acs$resurrected)[3],table(acs$resurrected,acs$size)[2,]),
round(c(median(acsize$months2res[acsize$resurrected],na.rm=T),aggregate(months2res~size,acsize[acsize$resurrected],median,na.rm=T)$months2res,NaN)),
c(length(pacsu$coin),table(pacsu$size)),
round(100*c(length(pacsu$coin)/sum(table(cvs$size)),table(pacsu$size)/table(cvs$size),0))
))

row.names(sumtbl)<-c("\\# tokens","\\# price peaks (total)", "\\# price peaks (median)", "\\# volume peaks (total)", "\\# volume peaks (median)","\\% price increase\\n 1st peak (median)","\\% volume increase\\n 1st peak (median)","\\# tokens abandoned","\\% tokens abandoned","\\# abandonments","days abandoned (median)","\\# tokens resurrected","\\% tokens resurrected","\\# resurrections","months to resurrected (median)","\\# tokens permanently\\\\abandoned","\\% tokens permanently\\\\abandoned")

write.table(sumtbl,"sumtbltex.txt",sep="&",row.names=T,quote=F)

ad2<-alivedead
ad2$month<-as.character(ad2$month)

bitcoin$month <- paste0(year(bitcoin$date), "-", month(bitcoin$date), "-01")
bitcoin$month <- as.Date(bitcoin$month)
bitcoin[, mprice:=mean(open), by="month"]
bitcoin$log10 <- log10(bitcoin$mprice)

numrespeak <- merge(numrespeak, unique(bitcoin[,c(8:9)]), by.x="month", by.y="month")
coins.avgvol <- coins.avgvol[, 4:5]
numrespeak <- merge(numrespeak, coins.avgvol, by.x="month", by.y="date")
corsub <- numrespeak[, c(2,10,3,12,11,5,6)]
colnames(corsub) <- c("coins_abandoned", "coins_resurrected", "coins_created", "trade_volume", "log_btc_price", "price_peaks", "volume_peaks")
tokencor <- rcorr(as.matrix(corsub))






